package com.lzy.filter;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import java.util.Collections;
import java.util.List;

/**
 * @description: 测试局部过滤器，TODO 待加逻辑
 * @author: Lzy
 * @create: 2020-11-26 17:28
 **/
@Slf4j
@Component
public class ResponseLogPartialGatewayFilterFactory extends AbstractGatewayFilterFactory<ResponseLogPartialGatewayFilterFactory.Config> {

    public ResponseLogPartialGatewayFilterFactory() {
        super(ResponseLogPartialGatewayFilterFactory.Config.class);
    }

    @Override
    public List<String> shortcutFieldOrder() {
        return Collections.singletonList("enabled");
    }

    @Override
    public GatewayFilter apply(Config config) {
        return (exchange, chain) -> chain.filter(exchange)
                .then(Mono.fromRunnable(() -> {
                    log.info("ResponseLogPartialGatewayFilterFactory，处理请求返回过滤器");
                    HttpHeaders httpHeaders = exchange.getResponse().getHeaders();
                    httpHeaders.remove("userInfo");
                    httpHeaders.remove("uuid");
                    httpHeaders.remove("appId");
                }));
    }

    @Data
    public static class Config {
        // 控制是否开启
        private boolean enabled;

        public Config() {
        }
    }
}
